package com.itheima.health.service.impl;

import com.itheima.health.dao.MemberMapper;
import com.itheima.health.exception.BusinessException;
import com.itheima.health.pojo.Member;
import com.itheima.health.service.MemberService;
import lombok.extern.slf4j.Slf4j;
import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import tk.mybatis.mapper.entity.Example;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;


@DubboService
@Slf4j
public class MemberServiceImpl implements MemberService {

    @Autowired
    MemberMapper memberMapper;
    /**
     * @Author: JASON
     * @Date: 8:50 2021/1/11
     * @Parms [member]
     * @ReturnType: void
     * @Description: 插入会员信息
    */
    @Override
    @Transactional
    public Integer add(Member member) {
        Member member2 = new Member();
        member2.setPhoneNumber(member.getPhoneNumber());
        Member member3 = memberMapper.selectOne(member2);

//        //判断是否存在然后存储
//        Member member1 = memberMapper.selectByPrimaryKey(member.getId());

        if(null!=member3){
            memberMapper.updateByPrimaryKeySelective(member);
            return member3.getId();
        }
        //存在则新建  赋值属性新建时间
        member.setRegTime(new Date());
        memberMapper.insertSelective(member);
        return member.getId();
    }

    @Override
    public List<Integer> countByMonth(List<String> months) {
        ArrayList<Integer> resultList = new ArrayList<>();
        for (String month : months) {
            //构造查询条件
            String endDate = month + ".31";
            int count = memberMapper.countByMonth(endDate);
            resultList.add(count);
            /*Example example = new Example(Member.class);
            Example.Criteria criteria = example.createCriteria();
            criteria.andLessThanOrEqualTo("regTime", endDate);
            int count = memberMapper.selectCountByExample(example);
            log.info("当前月份month:{},人数为：{}",month,count);
            resultList.add(count);*/
        }
        return resultList;
    }
}
